//
//  JXTabBarController.m
//

#import "JXTabBarController.h"

#import "JXMallViewController.h"
#import "JXMeViewController.h"

@interface JXTabBarController ()

@end

@implementation JXTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tabBar.backgroundColor = [UIColor clearColor];
    self.delegate = self;
    [self setupAllChildViewControllers];
}

- (void)setupAllChildViewControllers {
    JXMallViewController *mallVC = [[JXMallViewController alloc] init];
    UINavigationController *mallNav = [self setupChildViewController:mallVC
                                                               title:@"首页"
                                                           imageName:@"home"
                                                   selectedImageName:@"home_selected"];
    JXMeViewController *meVC = [[JXMeViewController alloc] init];
    UINavigationController *meNav = [self setupChildViewController:meVC
                                                             title:@"我"
                                                         imageName:@"me"
                                                 selectedImageName:@"me_selected"];
    [self setViewControllers:@[ mallNav, meNav ] animated:YES];
}

/**
 *  初始化一个子控制器
 *
 *  @param childVc           需要初始化的子控制器
 *  @param title             标题
 *  @param imageName         图标
 *  @param selectedImageName 选中的图标
 */
- (UINavigationController *)setupChildViewController:(UIViewController *)childVc
                                               title:(NSString *)title
                                           imageName:(NSString *)imageName
                                   selectedImageName:(NSString *)selectedImageName {
    // 设置控制器的属性
    childVc.title = title;
    childVc.view.backgroundColor = [UIColor whiteColor];
    // 设置图标
    childVc.tabBarItem.image =
            [JXImage(imageName) imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    childVc.tabBarController.tabBar.delegate = self;
    [childVc.tabBarItem setTitleTextAttributes:@{
        NSForegroundColorAttributeName : JXPureColor(118.0),
        NSFontAttributeName : JXSystemFont(14.0)
    }
                                      forState:UIControlStateNormal];
    //设置选中时的颜色
    [childVc.tabBarItem setTitleTextAttributes:@{
        NSForegroundColorAttributeName : JXColorWithRGB(239.0, 23.0, 58.0),
        NSFontAttributeName : JXSystemFont(14.0)
    }
                                      forState:UIControlStateSelected];

    // 设置选中的图标
    UIImage *selectedImage = [UIImage imageNamed:selectedImageName];
    childVc.tabBarItem.selectedImage =
            [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    // 包装一个导航控制器
    UINavigationController *nav =
            [[UINavigationController alloc] initWithRootViewController:childVc];
    return nav;
}

#pragma mark - UITabBarControllerDelegate 可在点击的时候切换样式或者其他效果

- (BOOL)tabBarController:(UITabBarController *)tabBarController
        shouldSelectViewController:(UIViewController *)viewController {
    return YES;
}

- (void)tabBarController:(UITabBarController *)tabBarController
 didSelectViewController:(UIViewController *)viewController {
    [((UIViewController *)[viewController.childViewControllers lastObject])
                    .navigationController popToRootViewControllerAnimated:NO];
}

- (void)tabBarController:(UITabBarController *)tabBarController
        willBeginCustomizingViewControllers:(NSArray *)viewControllers {
}
- (void)tabBarController:(UITabBarController *)tabBarController
        willEndCustomizingViewControllers:(NSArray *)viewControllers
                                  changed:(BOOL)changed {
}
- (void)tabBarController:(UITabBarController *)tabBarController
        didEndCustomizingViewControllers:(NSArray *)viewControllers
                                 changed:(BOOL)changed {
}

@end
